package servlet.events;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import servlet.ServletController;
import bundle.ErrorsBundleKey.RecipeErrorBundleKey;

import model.Recipe;
import dao.DAOFactory;

public class AddRecipeHandler extends EventHandler {	
	
	@Override
	public void execute(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {			
		
			if (validateForm(request)) //if ok
			{				
				if (DAOFactory.getRecipeDAO().insert(getRecipe(request)))
					request.setAttribute("status", "ok");
				else
					request.setAttribute("status", "error");
					
			}
				forward(urlBundle.getString("ADD_RECIPE_URL"), request, response);
			
	}	
	
	private boolean validateForm(HttpServletRequest request) {
		int i = 0;
		//ResourceBundle bundle = ResourceBundle.getBundle("Errors");
		
		//recipe name
		if(ServletController.getParameter(RECIPE_NAME) == null 
				|| ServletController.getParameter(RECIPE_NAME).trim().equals("")) {
			//request.setAttribute("RECIPE_NAME_NOT_NULL", bundle.getString("RECIPE_NAME_NOT_NULL"));
			setErrors(RecipeErrorBundleKey.RECIPE_NAME_NOT_NULL, request);
			i++;
		}
		
		//recipe description
		if (ServletController.getParameter(RECIPE_DESCRIPTION) == null 
				|| ServletController.getParameter(RECIPE_DESCRIPTION).trim().equals("")) {
			//request.setAttribute("RECIPE_DESCRIPTION_NOT_NULL", bundle.getString("RECIPE_DESCRIPTION_NOT_NULL"));
			setErrors(RecipeErrorBundleKey.RECIPE_DESCRIPTION_NOT_NULL, request);
			i++;
		}
		
		//people quantity
		try {
			if(ServletController.getParameter(RECIPE_QUANTITY_PEOPLE) == null) {
				//request.setAttribute("RECIPE_QUANTITY_PEOPLE_NOT_NULL", bundle.getString("RECIPE_QUANTITY_PEOPLE_NOT_NULL"));
				setErrors(RecipeErrorBundleKey.RECIPE_QUANTITY_PEOPLE_NOT_NULL, request);
				i++;
			}
			else if(Integer.parseInt(ServletController.getParameter(RECIPE_QUANTITY_PEOPLE)) == 0) {
				//request.setAttribute("RECIPE_QUANTITY_PEOPLE_NOT_ZERO", bundle.getString("RECIPE_QUANTITY_PEOPLE_NOT_ZERO"));
				setErrors(RecipeErrorBundleKey.RECIPE_QUANTITY_PEOPLE_NOT_ZERO, request);
				i++;
			}
		}
		catch (NumberFormatException nfe) {
			//request.setAttribute("RECIPE_QUANTITY_PEOPLE_FORMAT", bundle.getString("RECIPE_QUANTITY_PEOPLE_FORMAT"));
			setErrors(RecipeErrorBundleKey.RECIPE_QUANTITY_PEOPLE_FORMAT, request);
			i++;
		}
		
		//...
		//preparation + cooking time
		//...
		
		//recipe ingredient
		if (ServletController.getParameter(RECIPE_INGREDIENT) == null 
				|| ServletController.getParameter(RECIPE_INGREDIENT).trim().equals("")) {
			//request.setAttribute("RECIPE_INGREDIENT_NOT_NULL", bundle.getString("RECIPE_INGREDIENT_NOT_NULL"));
			setErrors(RecipeErrorBundleKey.RECIPE_INGREDIENT_NOT_NULL, request);
			i++;
		}
		
		//recipe preparation
		if (ServletController.getParameter(RECIPE_PREPARATION) == null 
				|| ServletController.getParameter(RECIPE_PREPARATION).trim().equals("")) {
			//request.setAttribute("RECIPE_PREPARATION_NOT_NULL", bundle.getString("RECIPE_PREPARATION_NOT_NULL"));
			setErrors(RecipeErrorBundleKey.RECIPE_PREPARATION_NOT_NULL, request);
			i++;
		}
		
		//image
		if (ServletController.getParameter(RECIPE_IMAGE) == null 
				|| ServletController.getParameter(RECIPE_IMAGE).trim().equals("")) {
			//request.setAttribute("RECIPE_IMAGE_NOT_NULL", bundle.getString("RECIPE_IMAGE_NOT_NULL"));
			setErrors(RecipeErrorBundleKey.RECIPE_IMAGE_NOT_NULL, request);
			i++;
		}
		
		//recipe category
		try {
			if (Integer.parseInt(ServletController.getParameter(RECIPE_CATEGORY)) == 0) {
				//request.setAttribute("RECIPE_CATEGORY_NOT_NULL", bundle.getString("RECIPE_CATEGORY_NOT_NULL"));
				setErrors(RecipeErrorBundleKey.RECIPE_CATEGORY_NOT_NULL, request);
				i++;
			}
		}
		catch (NumberFormatException nfe) {
			//request.setAttribute("RECIPE_CATEGORY_INVALID", bundle.getString("RECIPE_CATEGORY_INVALID"));
			setErrors(RecipeErrorBundleKey.RECIPE_CATEGORY_INVALID, request);
			i++;
		}
		
		if (i != 0)
			return false;
		
		return true;
	}		
	
	private Recipe getRecipe(HttpServletRequest request) {
		Recipe recipe = new Recipe();
		int hour, minute;
		
		//recipe name
		recipe.setName(ServletController.getParameter(RECIPE_NAME));
		
		//recipe description
		recipe.setDescription(ServletController.getParameter(RECIPE_DESCRIPTION));
		
		//number of people
		recipe.setQuantity_people(Integer.parseInt(ServletController.getParameter(RECIPE_QUANTITY_PEOPLE)));
		
		//preperation time
		hour = Integer.parseInt(ServletController.getParameter(RECIPE_TIME_PREPARATION_HOUR));
		minute = Integer.parseInt(ServletController.getParameter(RECIPE_TIME_PREPARATION_MINUTE));
		recipe.setTime_preparation(hour * 60 + minute);
		
		//cooking time
		hour = Integer.parseInt(ServletController.getParameter(RECIPE_TIME_COOKING_HOUR));
		minute = Integer.parseInt(ServletController.getParameter(RECIPE_TIME_COOKING_MINUTE));
		recipe.setTime_cooking(hour * 60 + minute);
		
		//ingredient
		recipe.setIngredient(ServletController.getParameter(RECIPE_INGREDIENT));
		
		//preperation
		recipe.setPreparation(ServletController.getParameter(RECIPE_PREPARATION));
		
		//recipe image
		recipe.setImage(ServletController.getParameter(RECIPE_IMAGE));
		
		//actor
		recipe.setUser_id(Integer.parseInt(ServletController.getParameter(USER_ID)));
		
		//recipe category
		recipe.setCategory_id(Integer.parseInt(ServletController.getParameter(RECIPE_CATEGORY)));
		
		return recipe;
	}	
	
	private static final String USER_ID = "user_id";
	private static final String RECIPE_NAME = "name";
	private static final String RECIPE_DESCRIPTION = "description";
	private static final String RECIPE_QUANTITY_PEOPLE = "quantity_people";
	private static final String RECIPE_TIME_PREPARATION_HOUR = "time_preparation_hour";
	private static final String RECIPE_TIME_PREPARATION_MINUTE = "time_preparation_minute";
	private static final String RECIPE_TIME_COOKING_HOUR = "time_cooking_hour";
	private static final String RECIPE_TIME_COOKING_MINUTE = "time_cooking_minute";
	private static final String RECIPE_INGREDIENT = "ingredient";
	private static final String RECIPE_PREPARATION = "preparation";
	private static final String RECIPE_IMAGE = "image";
	private static final String RECIPE_CATEGORY = "category";
}
